package com.liyu.controller;

import com.liyu.message.Request;
import com.liyu.message.Response;
import com.liyu.message.ResponseStatus;
import com.liyu.net.DataBuffer;
import com.liyu.net.OnlineClientIoCache;
import com.liyu.pojo.User;
import com.liyu.service.UserService;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 接收处理用户请求
 *
 * @author Yuhaotian
 * @date. 2022/10/31 15:00
 * @since 1.0.0
 */

public class RequestProcessor implements Runnable {
    /**
     * 客户端socket对象
     */
    private final Socket currentClientSocket;

    public RequestProcessor(Socket currentClientSocket) {
        this.currentClientSocket = currentClientSocket;
    }

    /**
     * 向指定客户端IO的输出流中输出指定响应
     */
    private static void sendResponseSys(OnlineClientIoCache onlineUserIo, Response response) throws IOException {
        ObjectOutputStream oos = onlineUserIo.getOos();
        oos.writeObject(response);
        oos.flush();
    }

    /** 拒绝接收文件 */
    /*private void refuseReceiveFile(Request request) throws IOException {
        FileInfo sendFile = (FileInfo)request.getAttribute("sendFile");
        Response response = new Response();  //创建一个响应对象
        response.setType(ResponseType.REFUSERECEIVEFILE);
        response.setData("sendFile", sendFile);
        response.setStatus(ResponseStatus.OK);
        //向请求方的输出流输出响应
        OnlineClientIOCache ocic = DataBuffer.onlineUserIOCacheMap.get(sendFile.getFromUser().getId());
        this.sendResponse(ocic, response);
    }*/

    /** 同意接收文件 */
    /*private void agreeReceiveFile(Request request) throws IOException {
        FileInfo sendFile = (FileInfo)request.getAttribute("sendFile");
        //向请求方(发送方)的输出流输出响应
        Response response = new Response();  //创建一个响应对象
        response.setType(ResponseType.AGREERECEIVEFILE);
        response.setData("sendFile", sendFile);
        response.setStatus(ResponseStatus.OK);
        OnlineClientIOCache sendIO = DataBuffer.onlineUserIOCacheMap.get(sendFile.getFromUser().getId());
        this.sendResponse(sendIO, response);

        //向接收方发出接收文件的响应
        Response response2 = new Response();  //创建一个响应对象
        response2.setType(ResponseType.RECEIVEFILE);
        response2.setData("sendFile", sendFile);
        response2.setStatus(ResponseStatus.OK);
        OnlineClientIOCache receiveIO = DataBuffer.onlineUserIOCacheMap.get(sendFile.getToUser().getId());
        this.sendResponse(receiveIO, response2);
    }*/

    /** 客户端退出 */
    /*public boolean logout(OnlineClientIoCache oio, Request request) throws IOException{
        System.out.println(currentClientSocket.getInetAddress().getHostAddress()
                + ":" + currentClientSocket.getPort() + "走了");

        User user = (User)request.getAttribute("user");
        //把当前上线客户端的IO从Map中删除
        DataBuffer.onlineUserIOCacheMap.remove(user.getId());
        //从在线用户缓存Map中删除当前用户
        DataBuffer.onlineUsersMap.remove(user.getId());

        Response response = new Response();  //创建一个响应对象
        response.setType(ResponseType.LOGOUT);
        response.setData("logoutUser", user);
        oio.getOos().writeObject(response);  //把响应对象往客户端写
        oio.getOos().flush();
        currentClientSocket.close();  //关闭这个客户端Socket

        DataBuffer.onlineUserTableModel.remove(user.getId()); //把当前下线用户从在线用户表Model中删除
        iteratorResponse(response);//通知所有其它在线客户端

        return false;  //断开监听
    }*/

    /**
     * 注册
     */
    /*public void registe(OnlineClientIoCache oio, Request request) throws IOException {
        User user = (User)request.getAttribute("user");
        UserService userService = new UserService();
        userService.addUser(user);

        Response response = new Response();  //创建一个响应对象
        response.setStatus(ResponseStatus.OK);
        response.setData("user", user);

        oio.getOos().writeObject(response);  //把响应对象往客户端写
        oio.getOos().flush();

        //把新注册用户添加到RegistedUserTableModel中
        DataBuffer.registedUserTableModel.add(new String[]{
                String.valueOf(user.getId()),
                user.getPassword(),
                user.getNickname(),
                String.valueOf(user.getSex())
        });
    }*/
    public void run() {
        //是否不间断监听
        boolean flag = true;
        try {
            OnlineClientIoCache currentClientIoCache = new OnlineClientIoCache(
                    new ObjectInputStream(currentClientSocket.getInputStream()),
                    new ObjectOutputStream(currentClientSocket.getOutputStream()));
            //不停地读取客户端发过来的请求对象
            while (flag) {
                //从请求输入流中读取到客户端提交的请求对象
                Request request = (Request) currentClientIoCache.getOis().readObject();
                System.out.println("Server读取了客户端的请求:" + request.getAction());
                //获取请求中的动作
                String actionName = request.getAction();

                if ("userRegiste".equals(actionName)) {
                    //用户注册
                    //registe(currentClientIoCache, request);
                } else if ("userLogin".equals(actionName)) {
                    //用户登录
                    login(currentClientIoCache, request);
                } else if ("exit".equals(actionName)) {
                    //请求断开连接
                    //flag = logout(currentClientIoCache, request);
                } else if ("chat".equals(actionName)) {
                    //聊天
                    //chat(request);
                } else if ("shake".equals(actionName)) {
                    //振动
                    //shake(request);
                } else if ("toSendFile".equals(actionName)) {
                    //准备发送文件
                    //toSendFile(request);
                } else if ("agreeReceiveFile".equals(actionName)) {
                    //同意接收文件
                    //agreeReceiveFile(request);
                } else if ("refuseReceiveFile".equals(actionName)) {
                    //拒绝接收文件
                    //refuseReceiveFile(request);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** 聊天 */
    /*public void chat(Request request) throws IOException {
        Message msg = (Message)request.getAttribute("msg");
        Response response = new Response();
        response.setStatus(ResponseStatus.OK);
        response.setType(ResponseType.CHAT);
        response.setData("txtMsg", msg);

        if(msg.getToUser() != null){ //私聊:只给私聊的对象返回响应
            OnlineClientIOCache io = DataBuffer.onlineUserIOCacheMap.get(msg.getToUser().getId());
            sendResponse(io, response);
        }else{  //群聊:给除了发消息的所有客户端都返回响应
            for(Long id : DataBuffer.onlineUserIOCacheMap.keySet()){
                if(msg.getFromUser().getId() == id ){	continue; }
                sendResponse(DataBuffer.onlineUserIOCacheMap.get(id), response);
            }
        }
    }*/

    /*广播*/
    /*public static void board(String str) throws IOException {
        User user = new User(1,"admin");
        Message msg = new Message();
        msg.setFromUser(user);
        msg.setSendTime(new Date());

        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        StringBuffer sb = new StringBuffer();
        sb.append(" ").append(df.format(msg.getSendTime())).append(" ");
        sb.append("系统通知\n  "+str+"\n");
        msg.setMessage(sb.toString());

        Response response = new Response();
        response.setStatus(ResponseStatus.OK);
        response.setType(ResponseType.BOARD);
        response.setData("txtMsg", msg);

        for (Long id : DataBuffer.onlineUserIOCacheMap.keySet()) {
            sendResponse_sys(DataBuffer.onlineUserIOCacheMap.get(id), response);
        }
    }*/

    /*踢除用户*/
    /*public static void remove(User user_) throws IOException{
        User user = new User(1,"admin");
        Message msg = new Message();
        msg.setFromUser(user);
        msg.setSendTime(new Date());
        msg.setToUser(user_);

        StringBuffer sb = new StringBuffer();
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        sb.append(" ").append(df.format(msg.getSendTime())).append(" ");
        sb.append("系统通知您\n  "+"您被强制下线"+"\n");
        msg.setMessage(sb.toString());

        Response response = new Response();
        response.setStatus(ResponseStatus.OK);
        response.setType(ResponseType.REMOVE);
        response.setData("txtMsg", msg);

        OnlineClientIOCache io = DataBuffer.onlineUserIOCacheMap.get(msg.getToUser().getId());
        sendResponse_sys(io, response);
    }*/

    /*私信*/
    /*public static void chat_sys(String str,User user_) throws IOException{
        User user = new User(1,"admin");
        Message msg = new Message();
        msg.setFromUser(user);
        msg.setSendTime(new Date());
        msg.setToUser(user_);

        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        StringBuffer sb = new StringBuffer();
        sb.append(" ").append(df.format(msg.getSendTime())).append(" ");
        sb.append("系统通知您\n  "+str+"\n");
        msg.setMessage(sb.toString());

        Response response = new Response();
        response.setStatus(ResponseStatus.OK);
        response.setType(ResponseType.CHAT);
        response.setData("txtMsg", msg);

        OnlineClientIOCache io = DataBuffer.onlineUserIOCacheMap.get(msg.getToUser().getId());
        sendResponse_sys(io, response);
    }*/

    /** 发送振动 */
    /*public void shake(Request request)throws IOException {
        Message msg = (Message) request.getAttribute("msg");

        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        StringBuffer sb = new StringBuffer();
        sb.append(" ").append(msg.getFromUser().getNickname())
                .append("(").append(msg.getFromUser().getId()).append(") ")
                .append(df.format(msg.getSendTime())).append("\n  给您发送了一个窗口抖动\n");
        msg.setMessage(sb.toString());

        Response response = new Response();
        response.setStatus(ResponseStatus.OK);
        response.setType(ResponseType.SHAKE);
        response.setData("ShakeMsg", msg);

        OnlineClientIOCache io = DataBuffer.onlineUserIOCacheMap.get(msg.getToUser().getId());
        sendResponse(io, response);
    }*/

    /** 准备发送文件 */
    /*public void toSendFile(Request request)throws IOException{
        Response response = new Response();
        response.setStatus(ResponseStatus.OK);
        response.setType(ResponseType.TOSENDFILE);
        FileInfo sendFile = (FileInfo)request.getAttribute("file");
        response.setData("sendFile", sendFile);
        //给文件接收方转发文件发送方的请求
        OnlineClientIOCache ioCache = DataBuffer.onlineUserIOCacheMap.get(sendFile.getToUser().getId());
        sendResponse(ioCache, response);
    }*/

    /**
     * 登录
     */
    public void login(OnlineClientIoCache currentClientIo, Request request) throws IOException {
        String idStr = (String) request.getAttribute("id");
        String password = (String) request.getAttribute("password");
        UserService userService = new UserService();
        User user = userService.login(idStr, password);
        //创建一个响应对象
        Response response = new Response();
        response.setStatus(ResponseStatus.OK);
        response.setData("user", user);
        //把响应对象往客户端写
        currentClientIo.getOos().writeObject(response);
        currentClientIo.getOos().flush();

    }

    /**
     * 给所有在线客户都发送响应
     */
    private void iteratorResponse(Response response) throws IOException {
        for (OnlineClientIoCache onlineUserIo : DataBuffer.onlineUserIOCacheMap.values()) {
            ObjectOutputStream oos = onlineUserIo.getOos();
            oos.writeObject(response);
            oos.flush();
        }
    }

    /**
     * 向指定客户端IO的输出流中输出指定响应
     */
    private void sendResponse(OnlineClientIoCache onlineUserIo, Response response) throws IOException {
        ObjectOutputStream oos = onlineUserIo.getOos();
        oos.writeObject(response);
        oos.flush();
    }
}
